---
#==============================================================#
# File      :   prometheus.yml
# Ctime     :   2020-04-03
# Mtime     :   2024-08-26
# Desc      :   Prometheus Configuration File
# Path      :   /etc/prometheus/prometheus.yml
# License   :   AGPLv3 @ https://doc.pgsty.com/about/license
# Copyright :   2018-2025  Ruohang Feng / Vonng (rh@vonng.com)
#==============================================================#

#--------------------------------------------------------------#
# Config FHS
#--------------------------------------------------------------#
# /etc/prometheus/
#  ^-----prometheus.yml    # prometheus main config file
#  ^-----alertmanager.yml  # alertmanger main config file
#  ^-----@bin              # util scripts: check,reload,status,new
#  ^-----@rules            # record & alerting rules definition
#
# {{ prometheus_sd_dir }}
#            ^-----@node   # node static targets definition
#            ^-----@ping   # ping targets definition
#            ^-----@pgsql  # pgsql static targets definition
#            ^-----@pgrds  # pgsql remote rds static targets
#            ^-----@redis  # redis static targets definition
#            ^-----@etcd   # etcd static targets definition
#            ^-----@minio  # minio static targets definition
#            ^-----@infra  # infra static targets definition
#            ^-----@mongo  # mongo static targets definition
#            ^-----@mysql  # mysql static targets definition
#            ^-----@docker # docker static targets definition
#--------------------------------------------------------------#


#--------------------------------------------------------------#
# Globals
#--------------------------------------------------------------#
global:
  scrape_interval: {{ prometheus_scrape_interval }}
  evaluation_interval: {{ prometheus_scrape_interval }}
  scrape_timeout: {{ prometheus_scrape_timeout }}


{% if alertmanager_enabled|bool %}
#--------------------------------------------------------------#
# Alerts
#--------------------------------------------------------------#
alerting:
  alertmanagers:
    - static_configs:
        - targets: [ {{ infra_portal.alertmanager.endpoint|replace('${admin_ip}', admin_ip) }} ]
      scheme: http
      timeout: 10s
      api_version: v2
{% endif %}


#--------------------------------------------------------------#
# Rules
#--------------------------------------------------------------#
rule_files:
  - rules/*.yml


#--------------------------------------------------------------#
# Targets Definition
#--------------------------------------------------------------#
# https://prometheus.io/docs/prometheus/latest/configuration/configuration/#file_sd_config
scrape_configs:

  #--------------------------------------------------------------#
  # job: push
  # pushgateway metrics
  #--------------------------------------------------------------#
  - job_name: push
    metrics_path: /metrics
    honor_labels: true
    static_configs: [{ targets: ['127.0.0.1:9091'] }]

  #--------------------------------------------------------------#
  # job: ping
  # blackbox exporter metrics
  #--------------------------------------------------------------#
  - job_name: ping
    metrics_path: /probe
    params: {module: [icmp]}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/ping/*.yml ]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        target_label: instance
      - target_label: __address__
        replacement: 127.0.0.1:9115

  #--------------------------------------------------------------#
  # job: infra
  # targets: prometheus | grafana | altermanager | loki | nginx
  # labels: [ip, instance, type]
  # path: targets/infra/<ip>.yml
  #--------------------------------------------------------------#
  - job_name: infra
    metrics_path: /metrics
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/infra/*.yml ]

  #--------------------------------------------------------------#
  # job: node
  # node_exporter, haproxy, docker, promtail
  # labels: [cls, ins, ip, instance]
  # path: targets/node/<ip>.yml
  #--------------------------------------------------------------#
  - job_name: node
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/node/*.yml ]

  #--------------------------------------------------------------#
  # job: docker
  # docker daemon
  # labels: [cls, ins, ip, instance]
  # path: targets/docker/<ip>.yml
  #--------------------------------------------------------------#
  - job_name: docker
    metrics_path: /metrics
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/docker/*.yml ]
    relabel_configs: # relabel docker metrics job to node
      - source_labels: [job]
        target_label: job
        replacement: 'node'

  #--------------------------------------------------------------#
  # job: etcd
  # labels: [cls, ins, ip, instance]
  # path: targets/etcd/<etcd_instance>.yml
  #--------------------------------------------------------------#
  - job_name: etcd
    metrics_path: /metrics
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/etcd/*.yml ]
    scheme: https
    tls_config:
      ca_file:   /etc/pki/ca.crt
      cert_file: /etc/pki/infra.crt
      key_file:  /etc/pki/infra.key

  #--------------------------------------------------------------#
  # job: minio
  # labels: [cls, ins, ip, instance]
  # path: targets/minio/<minio_instance>.yml
  #--------------------------------------------------------------#
  - job_name: minio
    metrics_path: /minio/v2/metrics/cluster
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/minio/*.yml ]
    scheme: https
    tls_config:
      ca_file:   /etc/pki/ca.crt
      cert_file: /etc/pki/infra.crt
      key_file:  /etc/pki/infra.key

  #--------------------------------------------------------------#
  # job: pgsql
  # pg_exporter | pgbouncer_exporter | patroni (if ssl disabled)
  # labels: [cls, ins, ip, instance]
  # path: targets/pgsql/<pg_instance>.yml
  #--------------------------------------------------------------#
  - job_name: pgsql
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/pgsql/*.yml ]

{% if patroni_ssl_enabled|bool %}
  #--------------------------------------------------------------#
  # job: patroni -> pgsql
  # labels: [cls, ins, ip, instance]
  # path: targets/patroni/<pg_instance>.yml
  #--------------------------------------------------------------#
  - job_name: pgsql-patroni     # this job is used when `patroni_ssl_enabled`
    honor_labels: true          # job name will be overwrite to 'pgsql' instead
    metrics_path: /metrics      # patroni can not customize exporter metrics
    scheme: https
    tls_config:
      ca_file: /etc/pki/ca.crt
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/patroni/*.yml ]
{% endif %}

  #--------------------------------------------------------------#
  # job: pgrds
  # pg_exporter on local
  # labels: [cls, ins, ip, job]
  # path: targets/pgrds/<pg_cluster>.yml
  #--------------------------------------------------------------#
  - job_name: pgrds
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/pgrds/*.yml ]

  #--------------------------------------------------------------#
  # job: redis
  # multiple redis targets from redis_exporter @ target node
  # labels: [cls, ip, ins, instance]
  # path: targets/redis/<redis_node>.yml
  #--------------------------------------------------------------#
  - job_name: redis
    metrics_path: /scrape
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/redis/*.yml ]
    relabel_configs:
      - source_labels: [__address__]
        target_label: __param_target
      - source_labels: [__param_target]
        regex: ^redis://(.*):(\d+)$
        replacement: $1:$2
        target_label: instance
      - source_labels: [__param_target]
        regex: ^redis://(.*):(\d+)$
        replacement: $1
        target_label: ip
      # scrape redis_exporter on target node
      - source_labels: [__param_target]
        regex: ^redis://(.*):\d+$
        replacement: $1:{{ redis_exporter_port }}
        target_label: __address__

  #--------------------------------------------------------------#
  # job: mysql
  # labels: [cls, ins, ip, instance]
  # path: targets/mysql/<mysql_instance>.yml
  #--------------------------------------------------------------#
  - job_name: mysql
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/mysql/*.yml ]

  #--------------------------------------------------------------#
  # job: mongo (ferretdb)
  # labels: [cls, ins, ip, instance]
  # path: targets/mongo/<mongo_instance>.yml
  #--------------------------------------------------------------#
  - job_name: mongo
    metrics_path: /debug/metrics
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/mongo/*.yml ]

  #--------------------------------------------------------------#
  # job: kafka
  # labels: [cls, ins, ip, instance]
  # path: targets/kafka/<kafka_instance>.yml
  #--------------------------------------------------------------#
  - job_name: kafka
    metrics_path: {{ exporter_metrics_path }}
    file_sd_configs:
      - refresh_interval: {{ prometheus_sd_interval }}
        files: [ {{ prometheus_sd_dir }}/kafka/*.yml ]


...